//
//  CXYMSGViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit
import SocketRocket

class CXYMSGViewController: CXYBaseTableViewController {
    
    var webSocket:SRWebSocket!
    var members:NSMutableArray!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.members = NSMutableArray()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let socket = (UIApplication.shared.delegate as! AppDelegate).socket
        socket?.delegate = self
        socket?.getOnlineMember()
    }
    
    // MARK: - Init
    
    override func setupUI() {
        self.title = "消息"
        let nib = UINib(nibName: "CXYMemberCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "CXYMemberCell")
        self.tableView.tableFooterView = UIView()
    }
}

extension CXYMSGViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.members == nil {
            return 0
        }
        return self.members.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CXYMemberCell", for: indexPath) as! CXYMemberCell
        cell.titleLabel.text = self.members[indexPath.row] as? String
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let vc = CXYChatViewController(nibName: "CXYChatViewController", bundle: nil)
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension CXYMSGViewController:CXYSocketDelegate {
    func getOnlineList(list: Array<Any>) {
        self.members.removeAllObjects()
        self.members.addObjects(from: list);
        self.tableView.reloadData()
    }
}
